package com.cuotoniu.lazybug.service;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cuotoniu.lazybug.model.LzUser;

@Service
public class LoginService {
	
	@Autowired
	private SessionFactory sessionFactory;
	
	public boolean checkLogin(String user,String pwd){
		Session s = getSession();
		Query qry = s.createQuery("from com.cuotoniu.lazybug.model.LzUser u where u.email = :name and u.password = :pwd ");
		qry.setString("name", user).setString("pwd", pwd);
		LzUser users = (LzUser)qry.uniqueResult();
		return users != null;
	}
	
	public void saveUser(){
		Session ss = getSession();
		Query qry = ss.createSQLQuery("insert into lz_user(email,mobile,password,remember) values(:email,:mobile,:password,:remember)");
		qry.setString("email", "diff_cc@163.com").setString("password", "test").setString("remember", "N").setString("mobile", "13610344622");
		int s = qry.executeUpdate();
		System.out.println("afaird rows:"+s);
	}
	
	 private Session getSession() {  
	    return sessionFactory.getCurrentSession();  
	 }
}
